var Utils = require('../../../../utils/util.js');
let WebIM = require("../../../../utils/webim")["default"];
let __test_account__, __test_psword__;
let disp = require("../../../../utils/broadcast");
let runAnimation = true
var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    questiontabs:0,
    allserve:false,
    shanchanglist: ["全部(550)", "气胸(16)", "肺癌(18)", "胸腔积液(5)", "肋骨骨折(4)", "心包积液(2)", "多汗症(1)"],
    pop_key: false,
    tabu_index: 1,
    ctab_scroll: [],
    ctab_scroll_index: 0,
    pop_time1: '',
    pop_time2: '',
    userInfo: {},
    hasUserInfo: false,
    // 主治擅长
    adepd: false,
    collect: false,
    parise: false,
    scrollleft: 0,
    username: "徐三",
    inquiry:""
  },
  //向医生问诊   0 图文问诊   1 视频问诊
  askdoctor(e){
    let inquiry = e.currentTarget.dataset.num;
    switch (inquiry){
      case '0': 
        wx.navigateTo({
          url: '/hyb_yl/zhuanjiasubpages/pages/zhuanjiatiwen/zhuanjiatiwen?inquiry=' + inquiry,
        })
        break;
      case '1':
        wx.navigateTo({
          url: '/hyb_yl/zhuanjiasubpages/pages/orderedtime/orderedtime?inquiry=' + inquiry,
        })
        break;
      case '2':
        wx.navigateTo({
          url: '/hyb_yl/zhuanjiasubpages/pages/orderedtime/orderedtime?inquiry=' + inquiry,
        })
        break;
      case '3':
        wx.navigateTo({
          url: '../chufangmes/chufangmes?inquiry=' + inquiry,
        })
        break;
      case '4':
        wx.navigateTo({
          url: '/hyb_yl/backstageServices/pages/kaiyao/kaiyao?inquiry=' + inquiry,
        })
        break;
      default:
        console.log(inquiry)
    }
    
  },
  //开通年卡
  yearcard(){
    wx.navigateTo({
      url: '/hyb_yl/backstageServices/pages/yearcard/yearcard',
    })
  },
  // 展开收起主治擅长
  fold: function () {
    this.setData({
      adepd: !this.data.adepd
    })
  },
  extract: function () {
    var username = this.data.username;
    var username = username.substr(0, 1) + '***';
    this.setData({
      username: username
    })
    console.log(this.data.username)
  },
  // 关注
  collect: function () {
    var that = this
    app.util.request({
      'url': 'entry/wxapp/Zhuanjia.changelove',
      data: {
        zid: that.data.zid,
        openid: wx.getStorageSync('openid'),
        cerated_type: 0
      },
      success: function (res) {
        console.log(res)
      }
    })
    this.setData({
      collect: !this.data.collect
    })
    if (this.data.collect == true) {
      wx.showToast({
        title: '关注成功',
        icon: 'none',
      })
    } else {

      wx.showToast({
        title: '取消关注成功',
        icon: 'none',
      })
    }
  },
  //全部服务弹框
  two_even: function (e) {
    this.setData({
      allserve: true,
    })
  },
  // g关闭服务弹框
  closeallserve(){
    this.setData({
      allserve: false,
    })
  },
  // 选择擅长
  questionclick(e){
    this.setData({
      questiontabs:e.currentTarget.dataset.dex
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.channel = "";
    this.uid = Utils.getUid();
    console.log(this.uid)
    this.lock = false;
    let userInfo = wx.getStorageSync("userInfo");
    if (userInfo) {
      this.setData({
        hasUserInfo: true,
        userInfo: userInfo
      });
    }
    var openid = wx.getStorageSync('openid')
    if (openid) {
      this.setData({
        isAuthor: true
      })
    } else {
      this.setData({
        isAuthor: false
      })
    }
    var that = this
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    app.util.request({
      'url': 'entry/wxapp/Index.base',
      success: function (res) {
        that.setData({
          show_title: res.data.show_title
        })
      }
    })
    var zid = options.zid
    var key = options.key
    var msg = options.msg
    var name = options.name
    that.setData({
      inquiry: options.inquiry,
      bgc: bgc,
      zid: zid,
      key: key,
      msg: msg,
      btn: key,
      name: name
    })
    that.getDocinfo()
    that.getguanzhu()
    that.getdocpingjia()
    this.extract()

    // 获取到屏幕的宽高等信息
    var maskWidth;
    wx: wx.getSystemInfo({
      success: function (res) {
        console.log(res)
        maskWidth = Math.round(res.screenWidth * 0.8)
        that.setData({
          windowWidth: res.windowWidth,
          windowHeight: res.windowHeight,
          maskWidth: maskWidth,
          maskHeight: Math.round(maskWidth * 1920 / 1080),
        })
      }
    })
    console.log(that.data.windowWidth, that.data.windowHeight)
    console.log(that.data.maskWidth, that.data.maskHeight)
  },

  likesClick(e) {
    console.log(e)
    var openid = wx.getStorageSync('openid')
    if (openid) {
      //登录环信
      var me = this
      me.setData({
        isAuthor: true
      })
    } else {
      me.setData({
        isAuthor: false
      })
    }
  },
  bindUsername: function (e) {
    this.setData({
      name: e.detail.value
    });
  },

  bindPassword: function (e) {
    this.setData({
      psd: e.detail.value
    });
  },
  onFocusPsd: function () {
    this.setData({
      psdFocus: 'psdFocus'
    })
  },
  onBlurPsd: function () {
    this.setData({
      psdFocus: ''
    })
  },
  onFocusName: function () {
    this.setData({
      nameFocus: 'nameFocus'
    })
  },
  onBlurName: function () {
    this.setData({
      nameFocus: ''
    })
  },
  login: function (e) {
    runAnimation = !runAnimation
    wx.setStorage({
      key: "myUsername",
      data: wx.getStorageSync('myUsername')
    });
    getApp().conn.open({
      apiUrl: WebIM.config.apiURL,
      user: wx.getStorageSync('myUsername'),
      pwd: '123',
      grant_type: this.data.grant_type,
      appKey: WebIM.config.appkey
    });
    console.log(wx.getStorageSync('myUsername'))
    var that = this
    var btn = that.data.btn
    console.log(btn)
    var randnum = that.data.randnum
    var docopenid = that.data.docopenid
    var useropenid = wx.getStorageSync('openid')
    if (docopenid == useropenid) {
      wx.showToast({
        title: '自己不能咨询自己',
        icon: 'none'
      })
      return false;
    }
    if (btn == 'dianhuawenzhen') {
      that.dianhuawenzhen(randnum)
    }
    if (btn == 'tuwenwenzhen') {
      that.tuwenwenzhen(randnum)
    }
    if (btn == 'sirenyisheng') {
      that.sirenyisheng(randnum)
    }
    if (btn == 'yuanchengwenzhen') {
      that.yuanchengwenzhen(randnum)
    }
    if (btn == 'zhuanjiatuandui') {
      that.zhuanjiatuandui(randnum)
    }
    if (btn == 'shoushukuaiyue') {
      that.shoushukuaiyue(randnum)
    }
    if (btn == 'tijianjiedu') {
      that.tijianjiedu(randnum)
    }
  },


  onGotUserInfo: function (e) {
    var that = this
    let userInfo = e.detail.userInfo || {};
    // store data for next launch use
    wx.setStorage({
      key: 'userInfo',
      data: userInfo,
    })
    var key = that.data.key
    if (key == 'tuwenwenzhen') {
      var my = wx.getStorageSync("myUsername");
      console.log(my)
      var nameList = {
        myName: my,
        your: 'jinjin123456789'
      };
      wx.navigateTo({
        url: '/hyb_yl/czhuanjiasubpages/pages/chatroom/chatroom?username=' + JSON.stringify(nameList),
      })
    } else {
      this.onJoin(userInfo);
    }
  },

  checkJoinLock: function () {
    return !(this.lock || false);
  },

  lockJoin: function () {
    this.lock = true;
  },

  unlockJoin: function () {
    this.lock = false;
  },

  onJoin: function (userInfo) {
    userInfo = userInfo || {};
    let value = this.channel || "123";

    let uid = this.uid;
    if (!value) {
      wx.showToast({
        title: '请提供一个有效的房间名',
        icon: 'none',
        duration: 2000
      })
    } else {
      if (this.checkJoinLock()) {
        this.lockJoin();
        let role = "audience";
        role = "broadcaster";
        wx.navigateTo({
          url: `../meeting/meeting?channel=${value}&uid=${uid}&role=${role}`
        });
      }
    }
  },
  onInputChannel: function (e) {
    let value = e.detail.value;
    this.channel = value;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.extract()
    //获取时间
    // 调用函数时，传入new Date()参数，返回值是日期和时间
    let time = Utils.formatTime(new Date());
    let day = parseFloat(time.slice(8, 10));
    let ctime = time.slice(5, 7) + '月' + day + '日';
    this.setData({
      pop_time1: ctime
    })
    for (var i = 0; i < 5; i++) {
      let times = time.slice(5, 7) + '月' + day + '日';
      day++;

      this.data.ctab_scroll.push(times)
      this.setData({
        ctab_scroll: this.data.ctab_scroll
      })
      // console.log(this.data.ctab_scroll) 
    }
  },
  // 首页
  navicate() {
    var that = this
    wx.reLaunch({
      url: '/hyb_yl/tabBar/index/index',
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  getDocinfo(e) {
    var that = this
    var zid = that.data.zid
    var key = that.data.btn
    app.util.request({
      'url': 'entry/wxapp/Zhuanjia.docinfo',
      data: {
        zid: zid,
        key: key
      },
      success: function (res) {
        console.log(res)
        wx.setNavigationBarTitle({
          title: res.data.data.z_name
        })
        var menu = res.data.menu
        var sid = res.data.data.sid
        var money_index = res.data.data.money
        for (var i = 0; i < menu.length; i++) {
          var index = sid.indexOf(menu[i].sid)
          menu[i].open = index
          menu[i].price = 20
          menu[i].pinyin2 = menu[i].pinyin
        }

        that.setData({
          docopenid: res.data.data.openid,
          docinfo: res.data.data,
          menu: res.data.menu,
          z_name: res.data.data.z_name,
          randnum: res.data.data.randnum
        })

      }
    })
  },
  dianhuawenzhen(randnum) {
    var that = this
    var zid = that.data.zid
    var name = that.data.name
    var keywords = that.data.btn
    wx.navigateTo({
      url: '/hyb_yl/czhuanjiasubpages/pages/telserev/index?zid=' + zid + '&keywords=' + keywords + '&name=' + name + '&randnum=' + randnum,
    })
  },
  tuwenwenzhen(randnum) {
    var that = this
    var name = that.data.name
    var zid = that.data.zid
    var keywords = that.data.btn
    wx.navigateTo({
      url: '/hyb_yl/zhuanjiasubpages/pages/zhuanjiatiwen/zhuanjiatiwen?zid=' + zid + '&keywords=' + keywords + '&name=' + name + '&randnum=' + randnum,
    })
  },
  sirenyisheng(randnum) {
    var that = this
    var zid = that.data.zid
    var name = that.data.name
    var keywords = that.data.btn
    wx.navigateTo({
      url: '/hyb_yl/czhuanjiasubpages/pages/perdoc/index?zid=' + zid + '&keywords=' + keywords + '&name=' + name + '&randnum=' + randnum,
    })
  },
  yuanchengwenzhen(randnum) {
    var that = this
    var zid = that.data.zid
    var name = that.data.name
    var keywords = that.data.btn
    wx.navigateTo({
      url: '/hyb_yl/czhuanjiasubpages/pages/longsevers/index?zid=' + zid + '&keywords=' + keywords + '&name=' + name + '&randnum=' + randnum,
    })
  },
  tijianjiedu(randnum) {
    var that = this
    var zid = that.data.zid
    var name = that.data.name
    var keywords = that.data.btn
    wx.navigateTo({
      url: '/hyb_yl/zhuanjiasubpages/pages/huanzhexinxi/huanzhexinxi?zid=' + zid + '&keywords=' + keywords + '&name=' + name + '&randnum=' + randnum,
    })
  },
  zhuanjiatuandui(randnum) {
    var that = this
    var zid = that.data.zid
    var name = that.data.name
    var keywords = that.data.btn
    wx.navigateTo({
      url: '/hyb_yl/doctor/pages/familydoctor/servercenter/servercenter?zid=' + zid + '&keywords=' + keywords + '&name=' + name + '&randnum=' + randnum,
    })
  },
  shoushukuaiyue(e) {
    var that = this
    var zid = that.data.zid
    var name = that.data.name
    var keywords = that.data.btn
    var money = that.data.money
    var z_name = that.data.z_name
    var hospital = that.data.hospital
    wx.navigateTo({
      url: '/hyb_yl/userCommunicate/pages/preindex/index/index?zid=' + zid + '&key=' + keywords + '&name=' + name + '&money=' + money + '&z_name=' + z_name + '&hospital=' + hospital + '&msg=立即预约',
    })
  },
  //是否关注
  getguanzhu() {
    var that = this
    app.util.request({
      'url': 'entry/wxapp/Zhuanjia.ifguanzhu',
      data: {
        zid: that.data.zid,
        openid: wx.getStorageSync('openid'),
        cerated_type: 0
      },
      success(res) {
        console.log(res)
        if (res.data == 1) {
          that.setData({
            collect: !that.data.collect
          })
        } else {
          that.setData({
            collect: that.data.collect
          })
        }
      }
    })
  },
  //查询医生评价
  getdocpingjia() {
    var that = this
    var zid = that.data.zid
    app.util.request({
      'url': 'entry/wxapp/Zhuanjia.pjlist',
      data: {
        zid: zid
      },
      success: function (res) {
        var list = res.data
        for (var i = 0; i < list.length; i++) {
          list[i].names = list[i].names.substr(0, 1) + '***';
        }
        that.setData({
          list: list
        })
        console.log(res)
      }
    })
  }
})